package test;
import java.util.Scanner;
public class dfs11 {
    static int[][] map,visit;
    static int startx,starty,endx,endy;
    static int min=Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n=scan.nextInt();
        map=new int[n][n];
        visit=new int[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                String s=scan.next();
                char into=s.charAt(0);

                if (into=='A'){
                    startx=i;
                    starty=j;
                    map[i][j]=3;


                }
                else if (into=='B'){
                    endx=i;
                    endy=j;
                    map[i][j]=0;
                }
                else if (into=='+'){
                    map[i][j]=1;
                }else{
                    map[i][j]=2;
                }
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
        visit[startx][starty]=1;
        dfs(n, startx,starty,0);
        System.out.println(min);
        scan.close();
    }
    public static void dfs(int n,int nx,int ny,int step){
        if(step<min && map[nx][ny]==0){
            min=step;
        }
        int[] x={1,0,-1,0};
        int[] y={0,1,0,-1};
        for(int i=0;i<4;i++){
            int nex=nx+x[i];
            int ney=ny+y[i];
            System.out.println(nex+" "+ney);
            if(0<=nex && nex<n && 0<=ney && ney<n &&  map[nex][ney]!=map[nx][ny] && step<min&&visit[nex][ney]==0){
                visit[nex][ney]=1;
                dfs(n,nex,ney,step+1);
                visit[nex][ney]=0;
            }
        }
    }
}
